#!/bin/bash


# -------------------------------------------------- The Bifrozt Honeypot Project 2014 -------------------------------------------------- #
#
#   Developer:      Are Hansen
#   Date:           2014, May 5
#   Version:        0.1.8
#
#   Usage:
#   This script is executed by the debconf installer and takes care of configuration, package
#   installation, updating and upgrading of Bifrozt during installation.
#
#   Copyright (c) 2014, Are Hansen - Honeypot Development
#
#   All rights reserved.
#
#   Redistribution and use in source and binary forms, with or without modification, are permitted
#   provided that the following conditions are met:
#
#   1. Redistributions of source code must retain the above copyright notice, this list of
#   conditions and the following disclaimer.
#
#   2. Redistributions in binary form must reproduce the above copyright notice, this list of
#   conditions and the following disclaimer in the documentation and/or other materials provided
#   with the distribution.
#
#   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND AN EXPRESS OR
#   IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
#   FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
#   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
#   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
#   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#   POSSIBILITY OF SUCH DAMAGE.
#
# -------------------------------------------------- The Bifrozt Honeypot Project 2014 -------------------------------------------------- #


unset DEBCONF_REDIR
unset DEBCONF_FRONTEND
unset DEBIAN_HAS_FRONTEND
unset DEBIAN_FRONTEND


declare Script="Bifrozt-installer"
declare Slog="/var/log/Bifrozt_Install.log"
declare honeyd="/usr/share/honeyd/scripts"
declare -rx chmod="/bin/chmod"
declare -rx chown="/bin/chown"
declare -rx cp="/bin/cp"
declare -rx date="/bin/date"
declare -rx echo="/bin/echo"
declare -rx grep="/bin/grep"
declare -rx mkdir="/bin/mkdir"
declare -rx mv="/bin/mv"
declare -rx rm="/bin/rm"
declare -rx aptget="/usr/bin/apt-get"
declare -rx cut="/usr/bin/cut"
declare -rx git="/usr/bin/git"
declare -rx touch="/usr/bin/touch"


function update_system()
{
    $aptget update &>/dev/null
    $aptget upgrade -y &>/dev/null
    $aptget dist-upgrade -y &>/dev/null
}


function install_bifrozt_pkgs()
{
    $aptget install git isc-dhcp-server htop slurm python-twisted python-mysqldb python-paramiko \
    geoip-bin geoip-database python-geoip honeyd iisemulator ttf-baekmuk ttf-arphic-gbsn00lp \
    ttf-arphic-bsmi00lp ttf-arphic-gkai00mp ttf-arphic-bkai00mp librrds-perl gawk tree -y
}


function git_bifrozt()
{
    $git clone https://are.hansen.floigir@code.google.com/p/bztest/ /root/bztest
}


function bifrozt_configs()
{
    cd /root/bztest
    $mv -v .git /.git

    cd /root/bztest/etc
    $cp -rv * /etc/

    cd /root/bztest/usr
    $cp -rv * /usr/
}


function git_honssh()
{
    $git clone https://code.google.com/p/honssh/ /opt/honssh
}


function user_setup()
{
    UN="$($grep '1000' /etc/passwd | $cut -d ':' -f1)"

    $cp -v /etc/skel/.bash_logout /home/$UN/.bash_logout

    if [ -e /home/$UN/.bash_logout ]
    then
        $chown -v 1000:1000 /home/$UN/.bash_logout
    fi

    $cp -v /etc/skel/.bashrc /home/$UN/.bashrc

    if [ -e /home/$UN/.bashrc ]
    then
        $chown -v 1000:1000 /home/$UN/.bashrc
    fi

    $cp -v /etc/skel/.profile /home/$UN/.profile
    
    if [ -e /home/$UN/.profile ]
    then
        $chown -v 1000:1000 /home/$UN/.profile
    fi

    $cp -v /etc/skel/README.info /home/$UN/README.info
    
    if [ -e /home/$UN/README.info ]
    then
        $chown -v 1000:1000 /home/$UN/README.info
    fi

    $cp -v /etc/skel/.vimrc /home/$UN/.vimrc

    if [ -e /home/$UN/.vimrc ]
    then
        $chown -v 1000:1000 /home/$UN/.vimrc
    fi
}


function clean_up()
{
   $touch /var/log/honeypot/honeyd.log

    $chmod -v 0755 /etc/cron.hourly/logrotate \
                   /etc/update-motd.d/00-header \
                   /etc/update-motd.d/10-help-text \
                   /etc/cron.hourly/logrotate \
                   $honeyd/unix/linux/suse8.0/ssh.sh \
                   $honeyd/unix/OSX/osx_ssh.sh \
                   $honeyd/misc/base.sh \
                   /usr/local/bin/bifrozt_stats \
                   /usr/local/bin/honsshctrl.sh

    $chmod -v 0644 /etc/rsyslog.d/13-iptables.conf \
                   /etc/ssh/sshd_config \
                   /etc/honeypot/honeyd.conf \
                   /etc/logrotate.d/iptables \
                   /usr/share/GeoIP/GeoIP.dat \
                   $Slog 


    $chmod -v 0666 /var/log/honeypot/honeyd.log

    $chmod -v 0777 /var/log/honeypot

    if [ -e /etc/update-motd.d/50-landscape-sysinfo ]
    then
        $rm -v  /etc/update-motd.d/50-landscape-sysinfo
    fi

    cd /root

    $rm -vrf bztest
}


$echo "$($date +"%Y  %b %d - %T") $Script[$$]: Bifrozt installation has started" >> $Slog

$echo "$($date +"%Y  %b %d - %T") $Script[$$]: Updating the system." >> $Slog
update_system
$echo "$($date +"%Y  %b %d - %T") $Script[$$]: System up to date." >> $Slog

$echo "$($date +"%Y  %b %d - %T") $Script[$$]: Installing Bifrozt base." >> $Slog
install_bifrozt_pkgs &>> $Slog
$echo "$($date +"%Y  %b %d - %T") $Script[$$]: Bifrozt base installed." >> $Slog

$echo "$($date +"%Y  %b %d - %T") $Script[$$]: Getting latest version of Bifrozt." >> $Slog
git_bifrozt &>> $Slog
$echo "$($date +"%Y  %b %d - %T") $Script[$$]: Latest Bifrozt version accuired." >> $Slog

$echo "$($date +"%Y  %b %d - %T") $Script[$$]: Installing configuration files." >> $Slog
bifrozt_configs &>> $Slog
$echo "$($date +"%Y  %b %d - %T") $Script[$$]: Configuration files installed." >> $Slog

$echo "$($date +"%Y  %b %d - %T") $Script[$$]: Installing HonSSH." >> $Slog
git_honssh &>> $Slog
$echo "$($date +"%Y  %b %d - %T") $Script[$$]: HonSSH installed." >> $Slog

$echo "$($date +"%Y  %b %d - %T") $Script[$$]: Configure user account." >> $Slog
user_setup >> $Slog
$echo "$($date +"%Y  %b %d - %T") $Script[$$]: User account configured." >> $Slog

$echo "$($date +"%Y  %b %d - %T") $Script[$$]: Running post-install actions." >> $Slog
clean_up &>> $Slog
$echo "$($date +"%Y  %b %d - %T") $Script[$$]: Post-install actions completed." >> $Slog

$echo "$($date +"%Y  %b %d - %T") $Script[$$]: Bifrozt installation is complete" >> $Slog


exit 0